package com.scoy.honeymei.holder;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.ocnyang.cartlayout.viewholder.CartViewHolder;
import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.BaseBean;
import com.scoy.honeymei.bean.GoodsOrderBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.LogUtil;
import com.scoy.honeymei.utils.MyUtil;

public abstract class ChildViewHolder extends CartViewHolder implements View.OnClickListener {
    public TextView textViewReduce;
    public TextView textViewNum;
    public TextView textViewAdd;

    public TextView delete;
    public ImageView cartImgIv;
    public TextView cartNameTv;
    public TextView cartPriceTv;


    public ChildViewHolder(View itemView, int chekbox_id) {
        super(itemView, chekbox_id);

        delete = itemView.findViewById(R.id.delete);
        cartImgIv = itemView.findViewById(R.id.cart_img_iv);
        cartNameTv = itemView.findViewById(R.id.cart_name_tv);
        cartPriceTv = itemView.findViewById(R.id.cart_price_tv);

        textViewReduce = ((TextView) itemView.findViewById(R.id.tv_reduce));
        textViewNum = itemView.findViewById(R.id.tv_num);
        textViewAdd = itemView.findViewById(R.id.tv_add);

        itemView.setOnClickListener(this);
        textViewReduce.setOnClickListener(this);
        textViewAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item:
                Toast.makeText(v.getContext(), ((GoodsOrderBean) mICartItem).getName(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_reduce:
                int intValue = Integer.valueOf(textViewNum.getText().toString()).intValue();
                if (intValue > 1) {
                    intValue--;
                    textViewNum.setText(String.valueOf(intValue));
                    ((GoodsOrderBean) mICartItem).setNumber(intValue);
                    onNeedCalculate();
                    httpCartChange(v.getContext(),((GoodsOrderBean) mICartItem).getGoodscarid(),2);
                }
                break;
            case R.id.tv_add:
                int intValue2 = Integer.valueOf(textViewNum.getText().toString()).intValue();
                intValue2++;
                textViewNum.setText(String.valueOf(intValue2));
                ((GoodsOrderBean) mICartItem).setNumber(intValue2);
                onNeedCalculate();
                httpCartChange(v.getContext(),((GoodsOrderBean) mICartItem).getGoodscarid(),1);
                break;
            default:
                break;
        }
    }

    /**
     * 这里因为把 ViewHolder 没有写到 adapter 中作为内部类，所以对事件写了一个回调的抽象方法。
     * 如果不想这样写，你可以在以下方式中选其一：
     * 1. 将 ViewHolder 写到 Adapter 中作为内部类，这样你就可以访问 Adapter 中的一些方法属性了；
     * 2. 或者，你把 ItemView & ItemChildView 的事件放到 Adapter 中的 onBindViewHolder() 方法中设置。
     */
    public abstract void onNeedCalculate();


    //修改购物车数量
    private void httpCartChange(Context context,int ids, int status) {
        HttpParams params = new HttpParams();
        params.put("id", ids);
        params.put("status", status);//1为增加 2为减少
        OkGo.<String>post(MyUrl.CART_CHANGE_NUM).params(params).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                LogUtil.e(body);
              /*  BaseBean baseBean = new Gson().fromJson(body, BaseBean.class);
                String code = baseBean.getCode();
                String msg = baseBean.getMsg();
*/
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                MyUtil.mytoast(context, response.toString());
            }
        });

    }
}
